var index = $("#index").val();
var type = $("#type").val();// 类型：1生产入库，2生产入库台账，3采购入库台账， 99999及时库存
var fEntryId = $("#fEntryId").val();
var fStockPlaceID = $("#fStockPlaceID").val();
var fBatchNoNew = $("#fBatchNoNew").val();
/*
 * @Description: 预加载
 * @param : 
 */
$(function (){
	if(type == 2){
		$("#titleName").text('销售单号');
		$("#supplier").text('客户');
		getPrint();
	}else if(type == 3){
		$("#titleName").text('进货单号');
		$("#supplier").text('供应商');
		getPrint();
	}else if(type == 99999){
		getERPprint();
	}
});
/*
 * @Description: 生产入库/采购入库打印
 * @param : 
 */
function getPrint(){
	console.log(index) 
	console.log(type) 
	$.ajax({
		url:pathUrl("temp/getItemInfo.action"),
		type:'POST',
		dataType:'JSON',
		data:{
			'fType' : type, 
			'id' : index,
			'fEntryId' : fEntryId
		},
		success:function(data){
			console.log(data)
//			var code = data.fItemID + "\n" + data.fName + "\n" + data.fNumber + "，" + data.fBatchNo + "，" + data.fQty;
//			var code = '名称:' + data.fName + "\n规格:" + nullFormatter(data.fModel) + "\n批次:" + nullFormatter(data.fBatchNo) + "\n数量:" + data.fQty;
			var code = [{
				'fItemID': nullFormatter(data.fItemID),
				'fStockID': nullFormatter(data.fStockID),
				'fBatchNo': nullFormatter(data.fBatchNo),
				'fStockPlaceID': nullFormatter(data.fStockPlaceID)
			}];
			var str = toUtf8(JSON.stringify(code));
			$("#fNumber").text(data.fNumber);//物料代码
			$("#fName").text(data.fName);//物料名称
			$("#fModel").text(nullFormatter(data.fModel));//规格型号
			$("#fOrderBillNo").text(nullFormatter(data.fOrderBillNo));//销售单号
			if(type == 2){
				$("#supplierName").text(nullFormatter(data.fCustName));//客户
			}else if(type == 3){
				$("#supplierName").text(nullFormatter(data.fSupplier));//供应商
			}
			$("#fDate").text(timeFormatter(data.fDate));//日期
			$("#fBatchNo").text(nullFormatter(data.fBatchNo));//批次号
			$("#fQty").text(data.fQty + nullFormatter(data.fUnitName));//数量
			
			$("#qrcode").qrcode({
				width: 95,
				height:95,
				correctLevel : 3,//容错级别
				text: str
			});
			setTimeout(function(){
				print();
			}, 500);
		}
	});
}
/*
 * @Description: 及时库存打印
 * @param : 
 */
function getERPprint(){
	$.ajax({
		url:pathUrl("temp/getInventoryInfo.action"),
		type:'POST',
		dataType:'JSON',
		data:{
			'fItemID' : index, 
			'fStockID' : fEntryId,
			'fStockPlaceID' : fStockPlaceID,
			'fBatchNo': fBatchNoNew
		},
		success:function(data){
			console.log(data)
			data = data.rows;
//			var code = data.fItemID + "\n" + data.fName + "\n" + data.fNumber + "，" + data.fBatchNo + "，" + data.fQty;
//			var code = '名称:' + data.fName + "\n规格:" + nullFormatter(data.fModel) + "\n批次:" + nullFormatter(data.fBatchNo) + "\n数量:" + data.fQty;
			var code = [{
				'fItemID': nullFormatter(data.fItemID),
				'fStockID': nullFormatter(data.fStockID),
				'fBatchNo': nullFormatter(data.fBatchNo),
				'fStockPlaceID': nullFormatter(data.fStockPlaceID)
			}];
			var str = toUtf8(JSON.stringify(code));
			$("#fNumber").text(data.fNumber);//物料代码
			$("#fName").text(data.fName);//物料名称
			$("#fModel").text(nullFormatter(data.fModel));//规格型号
			$("#fOrderBillNo").text(nullFormatter(data.fOrderBillNo));//销售单号
			if(data.fType == 0){ // 供应商
				$("#titleName").text('进货单号');
				$("#supplier").text('供应商');
				$("#supplierName").text(nullFormatter(data.fSupplier));
			}else{//客户
				$("#titleName").text('销售单号');
				$("#supplier").text('客户');
				$("#supplierName").text(nullFormatter(data.fCustName));
			}
			$("#fOrderBillNo").text(nullFormatter(data.fOrderBillNo));  // 进货单号或销售单号
			$("#fDate").text(timeFormatter(data.fDate));//日期
			$("#fBatchNo").text(nullFormatter(data.fBatchNo));//批次号
			$("#fQty").text(data.fQty + nullFormatter(data.fUnitName));//数量
			
			$("#qrcode").qrcode({
				width: 95,
				height:95,
				correctLevel : 3,//容错级别
				text: str
			});
			setTimeout(function(){
				print();
			}, 500);
		}
	});
}

/*
 * @Description: 打印
 * @param :
 */
function print() {
	var canvas=document.getElementsByTagName("canvas")[0];
	var dataURL = canvas.toDataURL("image/png");
	var img = '<img src="'+dataURL+'"></img>';
	$("#qrcode").html(img);
	$(".row").print({
		deferred: $.Deferred().done(function() { window.close(); })//打印完后自动关闭打印页面
	});
}
/*
 * @Description: 为null格式化
 * @param :
 */
function nullFormatter(value) {
	if(value == null) {
		return "";
	}
	return value;
}